package com.lincoln.jenkins.test;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ReceiveMsg {
    /// 发言人（群里和私聊通用，框架定义的）
    private String sender;
    /// 发言人昵称（群里和私聊通用，框架定义的）
    private String senderNick;
    /// 群聊时表示群名，否则空
    private String fromNick;
    /// at列表，群聊时用
    private List<String> atList;
    private Date createTime;
    /// 群消息和私聊消息都是同一个事件，因此FromId如果是@chatroom结尾就是群消息，否则为私聊。其他公众号等特殊id请自己过滤
    private String fromId;
    private int msgKey;
    /// 消息内容，可能包含XML消息，公众号消息等，请自行处理
    private String content;
    private String exInfo;
    private long serverId;
    private int status;
    private long svrId;
    /// 接收者
    private String toId;
    private int type;
    private String xmlContent;

    public ReceiveMsg(Map<String, String> params) {
        this.sender = params.get("Sender");
        this.senderNick = params.get("SenderNick");
        this.fromNick = params.get("FromNick");
        this.atList = new ArrayList<>();
        if (params.get("AtList") != null && !"".equals(params.get("AtList"))) {
            for (String at : params.get("AtList").split(",")) {
                atList.add(at);
            }
        }
        try {
            this.createTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(params.get("CreateTime"));
        } catch (ParseException e) {
            this.createTime = new Date();
        }
        this.fromId = params.get("FromId");
        this.msgKey = Integer.parseInt(params.get("msgKey"));
        this.content = params.get("Content");
        this.exInfo = params.get("ExInfo");
        this.serverId = Long.parseLong(params.get("ServerId"));
        this.status = Integer.parseInt(params.get("Status"));
        this.svrId = Long.parseLong(params.get("SvrId"));
        this.toId = params.get("ToId");
        this.type = Integer.parseInt(params.get("Type"));
        this.xmlContent = params.get("XmlContent");
    }
}
